﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml;

using Microsoft.SharePoint.Client;

namespace SharePointTagCloud.Silverlight
{
    public class RSSTagData : SharePointTagCloud.Silverlight.ITagData
    {
        private ListItemCollection listItems;
        private List list;
        public Dictionary<string, int> tags { get; set; }
        public List<TagLink> tagLinks { get; set; }
        public string LinkDetails { get; set; }
        private ClientContext context;
        private string defaultUrl;

        public event DataLoadEvent DataLoaded;

        public RSSTagData()
        {
            tags = new Dictionary<string, int>();
            tagLinks = new List<TagLink>();
        }

        public void GetData()
        {
            WebClient client = new WebClient();
            client.DownloadStringAsync(new Uri(LinkDetails));
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Exception exp = e.Error;
            }
            XmlReader reader = XmlReader.Create(new StringReader(e.Result));
            SyndicationFeed feed = SyndicationFeed.Load(reader);

            foreach (SyndicationItem item in feed.Items)
            {
                string wordText = StripHtmlTags(item.Summary.Text);
                string[] words = wordText.Split(' ');
                    
                    foreach (string word in words)
                    {
                        if (item.Links.Count > 0)
                        {
                            var matchingLinks = from tagLink in tagLinks
                                                where tagLink.Keyword == word
                                                && tagLink.Link == item.Links[0].Uri
                                                select tagLink;
                            if (matchingLinks.Count() == 0)
                            { tagLinks.Add(new TagLink(word, item.Links[0].Uri, StripHtmlTags(item.Title.Text))); }
                            if (!tags.ContainsKey(word))
                            { tags.Add(word, 1); }
                            else
                            { tags[word] = tags[word] + 1; }
                        }
                    }
                
                /*
                HyperlinkButton link = new HyperlinkButton();
                link.Content = item.Title.Text;
                link.NavigateUri = item.Links.First().Uri;
                link.TargetName = &amp; quot; _blank & amp; quot; ;
                LinkList.Children.Add(link);
                 * */
            }
            DataLoaded();
        }

        private string StripHtmlTags(string value)
        {
            int length = 0;
            int.TryParse(value, out length);

            // Remove HTML tags and empty newlines and spaces and leading spaces
            string formattedValue = Regex.Replace(value as string, "<.*?>", "");
            formattedValue = Regex.Replace(formattedValue, @"\n+\s+", "\n\n");
            formattedValue = formattedValue.TrimStart(' ');
            formattedValue = HttpUtility.HtmlDecode(formattedValue);
            if (length > 0 && formattedValue.Length >= length)
                formattedValue = formattedValue.Substring(0, length - 1);
            return formattedValue;
        }
    }
}
